﻿#region Copyright (c) 2013-05, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ViewCommand_T
	{
		[Test]
		public void Test_Usage_Execute()
		{
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				ignoreNullParameters: false);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));

			command.Execute(null);

			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo(null));

			var result = command.CanExecute(null);

			Assert.That(result, Is.True);

			command.Execute("BLAH");

			Assert.That(executeCalled, Is.EqualTo(2));
			Assert.That(executeArgument, Is.EqualTo("BLAH"));
		}

		[Test]
		public void Test_Usage_Execute_IgnoreNullParameters()
		{
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				ignoreNullParameters: true);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));

			command.Execute(null);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));

			var result = command.CanExecute(null);

			Assert.That(result, Is.False);

			command.Execute("BLAH");

			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo("BLAH"));
		}

		[Test]
		public void Test_Usage_Execute_After_Dispose()
		{
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				ignoreNullParameters: false);

			Assert.That(command.IsDisposed, Is.False);
			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));

			command.Execute("BLAH");

			Assert.That(command.IsDisposed, Is.False);
			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo("BLAH"));

			Helper.Dispose(command);
			command.Execute("BLUB");

			Assert.That(command.IsDisposed, Is.True);
			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo("BLAH"));
		}

		[Test]
		public void Test_Usage_CanExecute()
		{
			var canExecuteCalled = 0;
			var canExecuteArgument = String.Empty;
			var canExecuteResult = true;
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				arg =>
				{
					canExecuteCalled++;
					canExecuteArgument = arg;
					return canExecuteResult;
				},
				ignoreNullParameters: false);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(0));
			Assert.That(canExecuteArgument, Is.EqualTo(String.Empty));

			command.CanExecute(null);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo(null));

			command.Execute(null);

			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo(null));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo(null));

			command.CanExecute("BLAH");

			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo(null));
			Assert.That(canExecuteCalled, Is.EqualTo(2));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));

			command.Execute("BLUB");

			Assert.That(executeCalled, Is.EqualTo(2));
			Assert.That(executeArgument, Is.EqualTo("BLUB"));
			Assert.That(canExecuteCalled, Is.EqualTo(2));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));

			canExecuteResult = false;
			command.Execute("BLAH2");

			Assert.That(executeCalled, Is.EqualTo(3));
			Assert.That(executeArgument, Is.EqualTo("BLAH2"));
			Assert.That(canExecuteCalled, Is.EqualTo(2));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));
		}

		[Test]
		public void Test_Usage_CanExecute_IgnoreNullParameters()
		{
			var canExecuteCalled = 0;
			var canExecuteArgument = String.Empty;
			var canExecuteResult = true;
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				arg =>
				{
					canExecuteCalled++;
					canExecuteArgument = arg;
					return canExecuteResult;
				},
				ignoreNullParameters: true);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(0));
			Assert.That(canExecuteArgument, Is.EqualTo(String.Empty));

			command.CanExecute(null);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(0));
			Assert.That(canExecuteArgument, Is.EqualTo(String.Empty));

			command.Execute(null);

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(0));
			Assert.That(canExecuteArgument, Is.EqualTo(String.Empty));

			command.CanExecute("BLAH");

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));

			command.Execute("BLUB");

			Assert.That(executeCalled, Is.EqualTo(1));
			Assert.That(executeArgument, Is.EqualTo("BLUB"));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));

			canExecuteResult = false;
			command.Execute("BLAH2");

			Assert.That(executeCalled, Is.EqualTo(2));
			Assert.That(executeArgument, Is.EqualTo("BLAH2"));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));
		}

		[Test]
		public void Test_Usage_CanExecute_After_Dispose()
		{
			var canExecuteCalled = 0;
			var canExecuteArgument = String.Empty;
			var canExecuteResult = true;
			var executeCalled = 0;
			var executeArgument = String.Empty;

			var command = new ViewCommand<String>(
				arg =>
				{
					executeCalled++;
					executeArgument = arg;
				},
				arg =>
				{
					canExecuteCalled++;
					canExecuteArgument = arg;
					return canExecuteResult;
				},
				ignoreNullParameters: false);

			Assert.That(command.IsDisposed, Is.False);
			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(0));
			Assert.That(canExecuteArgument, Is.EqualTo(String.Empty));

			var result1 = command.CanExecute("BLAH");

			Assert.That(command.IsDisposed, Is.False);
			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));
			Assert.That(result1, Is.True);

			Helper.Dispose(command);
			var result2 = command.CanExecute("BLUB");

			Assert.That(executeCalled, Is.EqualTo(0));
			Assert.That(executeArgument, Is.EqualTo(String.Empty));
			Assert.That(canExecuteCalled, Is.EqualTo(1));
			Assert.That(canExecuteArgument, Is.EqualTo("BLAH"));
			Assert.That(result2, Is.False);
		}

		[Test]
		public void Test_Usage_CanExecuteChanged_Without_CanExecuteHandler()
		{
			var command = new ViewCommand<String>(
				arg =>
				{
				});

			var handlerCalled = 0;
			EventHandler handler = (sender, e) =>
				handlerCalled++;

			command.CanExecuteChanged += handler;
			command.RaiseCanExecuteChanged();

			//Assert.That(handlerCalled, Is.EqualTo(0));		// DOES NOT WORK WITHOUT UI

			command.CanExecuteChanged -= handler;
		}

		[Test]
		public void Test_Usage_CanExecuteChanged_With_CanExecuteHandler()
		{
			var command = new ViewCommand<String>(
				arg =>
				{
				},
				arg =>
				{
					return false;
				});

			Int32 handlerCalled = 0;
			EventHandler handler = (sender, e) =>
				handlerCalled++;

			command.CanExecuteChanged += handler;
			command.RaiseCanExecuteChanged();

			//Assert.That(handlerCalled, Is.EqualTo(1));		// DOES NOT WORK WITHOUT UI

			command.CanExecuteChanged -= handler;
		}

	}
}

#endif
